package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.Bank;
import com.ipfsemc.ipfsemc.model.BankName;
import com.ipfsemc.ipfsemc.service.IBankNameService;
import com.ipfsemc.ipfsemc.service.imp.IBankService;
import com.ipfsemc.ipfsemc.swagger.ApiModelPropertyIgnore;
import com.ipfsemc.ipfsemc.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("bank")
@Api(value = "BankController", tags = "银行卡")

public class BankController {


    private static final Logger logger = LoggerFactory.getLogger(BankController.class);
    @Autowired
    private IBankService bankService;

    @Autowired
    private IBankNameService bankNameService;
    @Autowired
    private PhoneCode phoneCode;

    @GetMapping("seletBank")
    @ApiOperation(value = "查询单个银行卡", notes = "说明",  httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="bankId",value="银行卡ID",required=true,dataType="Long")
    })
    public Response<Bank> selectBank(Long bankId){
        Response jsonData = new Response<>();
        try {
            if (null == bankId) {
                jsonData.setMessage("参数异常！");
            }else{
                Bank b = bankService.selectBank(bankId);
                jsonData.setResult(b);
                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[BankController][seletBank]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "添加银行卡", notes = "说明",  httpMethod = "POST")

    @RequestMapping(value = "addBank", method = RequestMethod.POST)
    public ResponseBase addBank(
            @ApiModelPropertyIgnore("bankId")
            @RequestBody Bank bank) {
//        String host = "https://yhkyz.market.alicloudapi.com";
//        String path = "/integrated_bankcard/check";
//        String method = "GET";
//        String appcode = "9fda6bab5c0646369d8698f50bc8696d";
//        Map<String, String> headers = new HashMap<String, String>();

        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == bank) {
                jsonData.setMessage("参数异常！");
            }else{
                jsonData=phoneCode.sureCode(bank.getBankPhone(),bank.getCode());

                if(null!=jsonData.getCode()&&jsonData.getCode()==0){
                    Bank mybank = bankService.selectByNum(bank);
                    if (null==mybank){
                        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//                        headers.put("Authorization", "APPCODE " + appcode);
//                        Map<String, String> querys = new HashMap<String, String>();
//                        querys.put("bankcard", bank.getBankNum());
//                        querys.put("idCard", bank.getUserCard());
//                        querys.put("mobile", bank.getBankPhone());
//                        querys.put("name", bank.getCnname());
//                        HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

//                    添加至数据库
                        int add = bankService.add(bank);
//                        JSONObject json = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
//                        return json;
                        if(add<0){
                            jsonData.setMessage("操作失败！");
                        }else{
                            jsonData.setCode(0);
                            jsonData.setMessage("操作成功！");
                        }

                    }else {
                        jsonData.setMessage("此卡已存在！");
                    }
                }

            }

        } catch (Exception ex) {
            logger.error("[BankController][applyAdd]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }


    @ApiOperation(value = "删除银行卡", notes = "说明",  httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="bankId",value="银行卡ID",required=true,dataType="Long")
    })
    @RequestMapping(value = "delBank", method = RequestMethod.POST)
    public ResponseBase delBank(Long bankId) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == bankId) {
                jsonData.setMessage("参数异常！");
            }else{
                int del = bankService.del(bankId);
                if (del>0){
                    jsonData.setMessage("删除成功！");
                    jsonData.setCode(0);
                }else {
                    jsonData.setMessage("失败");
                }

            }

        } catch (Exception ex) {
            logger.error("[BankController][delBank]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "修改银行卡", notes = "说明",  httpMethod = "POST")

    @RequestMapping(value = "updateBank", method = RequestMethod.POST)
    public ResponseBase updateBank(@ApiModelPropertyIgnore("userId")
                                   @RequestBody Bank bank) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == bank.getBankId()) {
                jsonData.setMessage("参数异常！");
            }else{
                jsonData=phoneCode.sureCode(bank.getBankPhone(),bank.getCode());

                if(null!=jsonData.getCode()&&jsonData.getCode().equals(0)){
                    bankService.update(bank);
                    jsonData.setMessage("操作成功！");
                }

            }

        } catch (Exception ex) {
            logger.error("[BankController][updateBank]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

    @ApiOperation(value = "查询用户所有银行卡", notes = "说明",  httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    @RequestMapping(value = "listUserBank", method = RequestMethod.GET)
    public RespList<Bank> listUserBank(String userId) {
        RespList jsonData = new RespList();

        try {
            if (null==userId){
                jsonData.setMessage("参数有误！");
            }
            else{
                Bank b=new Bank();
                b.setUserId(userId);
                List<Bank> list = bankService.list(b);

                jsonData.setResult(list);

                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[BankController][listUserBank]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }


    @ApiOperation(value = "查询所有银行卡名", notes = "说明",  httpMethod = "GET")
    @GetMapping("listBankName")
    public RespList<BankName> listBankName(){
        RespList jsonData=new RespList();
        List<BankName> bankNames = bankNameService.listBankName();
        jsonData.setResult(bankNames);
        jsonData.setCode(0);
        return jsonData;
    }
}
